package cn.edu.stu.syllabus.oa.detail;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.view.MenuItem;

import com.like.LikeButton;
import com.like.OnLikeListener;

import javax.inject.Inject;

import butterknife.BindView;
import cn.edu.stu.syllabus.R;
import cn.edu.stu.syllabus.base.BaseActivity;
import cn.edu.stu.syllabus.repository.oa.local.OaInfoEntry;
import cn.edu.stu.syllabus.utils.ActivityUtils;
import cn.edu.stu.syllabus.utils.ExtraNameJoiner;
import cn.edu.stu.syllabus.utils.GuideHelper;

public class OaDetailActivity extends BaseActivity implements Toolbar.OnMenuItemClickListener {

    private static final String EXTRA_OA_INFO = ExtraNameJoiner.join(OaDetailActivity.class
            , OaInfoEntry.class.getSimpleName());

    public static final int RESULT_STAR_STATE_CHANGE = 200;

    public static final String EXTRA_IS_STAR = ExtraNameJoiner.join(OaDetailActivity.class
            , "isStar");

    Intent resultIntent;

    @Inject
    OaDetailPresenter mOaDetailPresenter;
    @BindView(R.id.likeButton)
    LikeButton mLikeButton;

    public interface OnStarButtonStateChangeListener {
        boolean isStar();

        void onStateChange(boolean isStar);
    }

    public interface OnScreenshotClickListener {
        void onScreenshotClick();
    }

    OnStarButtonStateChangeListener mOnStarButtonStateChangeListener;

    OnScreenshotClickListener mOnScreenshotClickListener;

    @Override
    protected int getContentViewId() {
        return R.layout.activity_oa_detail;
    }

    @Override
    protected void init(Bundle savedInstanceState) {
        setTitle("详情");

        mToolbar.inflateMenu(R.menu.menu_oa_detail);
        mToolbar.setOnMenuItemClickListener(this);

        OaDetailFragment oaDetailFragment = (OaDetailFragment) getSupportFragmentManager()
                .findFragmentById(R.id.contentFrame);
        if (oaDetailFragment == null) {
            oaDetailFragment = new OaDetailFragment();
            ActivityUtils.addFragmentToActivity(
                    getSupportFragmentManager(), oaDetailFragment, R.id.contentFrame);
        }

        OaInfoEntry oaInfoEntry = getIntent().getParcelableExtra(EXTRA_OA_INFO);

        DaggerOaDetailComponent.builder()
                .oaDetailPresenterModule(new OaDetailPresenterModule(oaDetailFragment, oaInfoEntry))
                .build().inject(this);

        resultIntent = new Intent();

        setOnStarButtonStateChangeListener(oaDetailFragment);
        if (mOnStarButtonStateChangeListener != null) {
            mLikeButton.setLiked(mOnStarButtonStateChangeListener.isStar());
        }
        mLikeButton.setOnLikeListener(new OnLikeListener() {
            @Override
            public void liked(LikeButton likeButton) {
                if (mOnStarButtonStateChangeListener != null) {
                    mOnStarButtonStateChangeListener.onStateChange(true);
                    setResultIsStar(true);
                }
            }

            @Override
            public void unLiked(LikeButton likeButton) {
                if (mOnStarButtonStateChangeListener != null) {
                    mOnStarButtonStateChangeListener.onStateChange(false);
                    setResultIsStar(false);
                }
            }
        });

        setOnScreenshotClickListener(oaDetailFragment);

        GuideHelper.startActSeqNow(this,
                GuideHelper.getToolbarItemViewTapTarget(mLikeButton,
                        "这是收藏开关按钮",
                        "点击后, 你可以将该办公自动化信息(加入/取出)收藏夹")
                        .id(1),
                GuideHelper.getToolbarItemTapTarget(mToolbar, R.id.action_screenshot,
                        "这是办公自动化截图的按钮",
                        "点击后, 你可以截取该办公自动化信息并保存或者分享")
                        .id(2)
        );
    }

    public static Intent newIntent(Context context, OaInfoEntry oaInfoEntry) {
        Intent intent = new Intent(context, OaDetailActivity.class);
        intent.putExtra(EXTRA_OA_INFO, oaInfoEntry);
        return intent;
    }

    @Override
    public boolean onMenuItemClick(MenuItem item) {
        int id = item.getItemId();
        if (id == R.id.action_screenshot) {
            if (mOnScreenshotClickListener != null) {
                mOnScreenshotClickListener.onScreenshotClick();
            }
        }
        return true;
    }

    public OnStarButtonStateChangeListener getOnStarButtonStateChangeListener() {
        return mOnStarButtonStateChangeListener;
    }

    public void setOnStarButtonStateChangeListener(OnStarButtonStateChangeListener onStarButtonStateChangeListener) {
        mOnStarButtonStateChangeListener = onStarButtonStateChangeListener;
    }

    public OnScreenshotClickListener getOnScreenshotClickListener() {
        return mOnScreenshotClickListener;
    }

    public void setOnScreenshotClickListener(OnScreenshotClickListener onScreenshotClickListener) {
        mOnScreenshotClickListener = onScreenshotClickListener;
    }

    private void setResultIsStar(boolean isStar) {
        resultIntent.putExtra(EXTRA_IS_STAR, isStar);
        setResult(RESULT_STAR_STATE_CHANGE, resultIntent);
    }

}
